REBOL [
	Title: "RebGUI functions"
	Owner: "Ashley G Truter"
	Purpose: "Accessor functions."
	Acknowledgements: {
		original ttf font code based on http://www.reboltech.com/library/scripts/get-fonts-windows.r
		disable/enable functions based on http://www.rebol.org/view-script.r?script=disable-face.r
	}
]

;
;	Window feel
;

view*/window-feel: make object! [
	redraw: none
	detect: make function! [face event /local f] [
		;	filter events outside of modal dialog
		all [
			view*/pop-face
			view*/pop-face/type = 'modal
			view*/pop-face <> face
			exit
		]
		;	unfocus?
		all [
			find [down alt-down up alt-up] event/type
			;	hide choose?
			all [
				view*/pop-face
				view*/pop-face/type = 'choose
				not within? event/offset (win-offset? view*/pop-face) - as-pair 0 sizes/line view*/pop-face/size + as-pair 0 sizes/line
				hide-popup
			]
			view*/focal-face
			not within? event/offset win-offset? view*/focal-face view*/focal-face/size
			unless edit/unfocus [exit]
		]
		switch event/type [
			key [
				case [
					event/key = #"^-" [
						if view*/focal-face [
							;	find previous/next tabbable field
							f: either event/shift [edit/back-field view*/focal-face] [edit/next-field view*/focal-face]
							all [
								find behaviors/action-on-tab view*/focal-face/type
								get in view*/focal-face 'action
								view*/focal-face/action/on-click view*/focal-face
							]
							all [:f set-focus f]
							exit
						]
					]
					find [#" " #"^M"] event/key [
						all [
							view*/focal-face view*/focal-face/type = 'button
							get in view*/focal-face 'action
							view*/focal-face/action/on-click view*/focal-face
							exit
						]
					]
					all [
						find [f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12] event/key
						get in on-fkey event/key
						on-fkey/(event/key) face event
						exit
					]
					;	if key is assigned to a click action do it
					any [not view*/focal-face view*/focal-face/type = 'button] [
						either f: select face/data event/key [
							all [
								get f 'action
								f/action/on-click f
								exit
							]
						] [
							if event/key = #"^[" [
								;	Hide modal dialog/popup
								unless empty? view*/pop-list [hide-popup exit]
								;	ignore if this is main window
								all [1 = length? screen*/pane exit]
								;	exit if closer action returns false
								all [get in face 'action not face/action face exit]
								;	prompt to quit if first window of multiple is closed
								all [
									face = pick screen*/pane 1
									1 <> length? screen*/pane
									either confirm "Do you really want to quit?" [quit] [exit]
								]
								undisplay face
								exit
							]
						]
					]
				]
			]
;			move [
;				;	hide choose on away
;				all [
;					view*/pop-face
;					view*/pop-face/type = 'choose
;					not within? event/offset (win-offset? view*/pop-face) - as-pair 0 sizes/line view*/pop-face/size + as-pair 0 sizes/line
;					hide-popup
;					exit
;				]
;			]
			resize [
				all [face/size <> face/span-size span-resize face face/size - face/span-size]
				show face
				face/span-size: face/size
				exit
			]
			close [
				;	unfocus
				if view*/focal-face [view*/focal-face: view*/caret: none edit/unlight-text]
				;	exit if closer action returns false
				all [get in face 'action not face/action face exit]
				;	exit modal dialog wait state
				all [face/type = 'modal hide-popup exit]
				;	prompt to quit if first window of multiple is closed
				all [
					face = pick screen*/pane 1
					1 <> length? screen*/pane
					either confirm "Do you really want to quit?" [quit] [exit]
				]
			]
		]
		event
	]
	over: none
	engage: none
]

functions: make object! [
	#include %functions/clear-text.r
	#include %functions/disable.r
	#include %functions/display.r
	#include %functions/do-events.r
	#include %functions/enable.r
	#include %functions/examine.r
	#include %functions/get-fonts.r
	#include %functions/get-values.r
	#include %functions/hide-popup.r
	#include %functions/in-widget.r
	#include %functions/layout.r
	#include %functions/set-color.r
	#include %functions/set-data.r
	#include %functions/set-focus.r
	#include %functions/set-locale.r
	#include %functions/set-state.r
	#include %functions/set-text.r
	#include %functions/set-text-color.r
	#include %functions/set-texts.r
	#include %functions/set-title.r
	#include %functions/show-popup.r
	#include %functions/translate.r
	#include %functions/undisplay.r
]

foreach word find first functions 'clear-text [
	set to word! word get in functions word
]